﻿namespace Shanda.DevFx.Security.Web.Pages
{
    using Shanda.DevFx.Core;
    using Shanda.DevFx.Data.Attributes;
    using Shanda.DevFx.Data.Utils;
    using Shanda.DevFx.Security;
    using Shanda.DevFx.Security.Web;
    using Shanda.DevFx.Utils;
    using System;
    using System.Web.UI.WebControls;

    public class GroupEditPage : Shanda.DevFx.Security.Web.BasePage
    {
        protected LinkButton btnAddRoles;
        protected LinkButton btnRemoveRoles;
        protected Button btnSubmit;
        protected CheckBox cbxAddMeToGroup;
        [Column("IsAvailable", ColumnType=typeof(bool))]
        protected DropDownList ddlIsAvailable;
        [Column("IsInnerGroup", ColumnType=typeof(bool))]
        protected DropDownList ddlIsInnerGroup;
        protected string groupNo;
        [Column("ParentNo")]
        protected HyperLink hlkParentNo;
        [Column("LayerIndex", ColumnType=typeof(int), WriteOnly=true)]
        protected Label lblLayerIndex;
        protected Label lblMessage;
        protected ListBox lbxAailRoles;
        protected ListBox lbxRoles;
        protected bool newItem;
        protected string parentNo;
        protected RequiredFieldValidator rfvGroupNo;
        protected RequiredFieldValidator rfvTitle;
        [Column("Description")]
        protected TextBox txtDescription;
        protected TextBox txtGroupNo;
        [Column("Title")]
        protected TextBox txtTitle;
        protected ValidationSummary vsGroup;

        private void btnAddRoles_Click(object sender, EventArgs e)
        {
            string str = null;
            for (int i = 0; i < this.lbxAailRoles.get_Items().get_Count(); i++)
            {
                ListItem item = this.lbxAailRoles.get_Items().get_Item(i);
                if (item.get_Selected() && !this.lbxRoles.get_Items().Contains(item))
                {
                    if (!this.newItem)
                    {
                        IAOPResult result = Securitor.GroupManager.AddGroupToRole(this.groupNo, item.get_Value());
                        if (result.IsFailed)
                        {
                            str = str + "<br>添加角色到组失败：" + result.ResultDescription;
                            i++;
                            goto Label_0086;
                        }
                    }
                    this.lbxRoles.get_Items().Add(item);
                Label_0086:;
                }
            }
            this.lblMessage.set_Text(str);
        }

        private void btnRemoveRoles_Click(object sender, EventArgs e)
        {
            string str = null;
            int num = 0;
            while (num < this.lbxRoles.get_Items().get_Count())
            {
                ListItem item = this.lbxRoles.get_Items().get_Item(num);
                if (item.get_Selected())
                {
                    if (!this.newItem)
                    {
                        IAOPResult result = Securitor.GroupManager.RemoveGroupFromRole(this.groupNo, item.get_Value());
                        if (result.IsFailed)
                        {
                            str = str + "<br>组中移除角色失败：" + result.ResultDescription;
                            num++;
                            continue;
                        }
                    }
                    this.lbxRoles.get_Items().Remove(item);
                }
                else
                {
                    num++;
                }
            }
            this.lblMessage.set_Text(str);
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            this.DoFormSubmit();
        }

        protected virtual void DoFormSubmit()
        {
            if (!base.get_IsValid())
            {
                this.lblMessage.set_Text("请正确填写表单");
            }
            else
            {
                Group group;
                if (this.newItem)
                {
                    if (Checker.CheckEmptyString("ParentNo", this.parentNo, false))
                    {
                        group = Securitor.GroupManager.CreateItem();
                    }
                    else
                    {
                        group = Securitor.GroupManager.CreateItem(this.parentNo);
                    }
                    group.GroupNo = this.txtGroupNo.get_Text();
                }
                else
                {
                    group = Securitor.GroupManager.GetItem(this.groupNo);
                }
                DataTransfer.TransWebFormDataToEntityProperty(this, group);
                IAOPResult result = group.Save();
                string resultDescription = null;
                if (result.IsFailed)
                {
                    resultDescription = result.ResultDescription;
                }
                else
                {
                    resultDescription = "成功保存";
                }
                if (this.newItem)
                {
                    foreach (ListItem item in this.lbxRoles.get_Items())
                    {
                        result = Securitor.GroupManager.AddGroupToRole(group.GroupNo, item.get_Value());
                        if (result.IsFailed)
                        {
                            resultDescription = resultDescription + "<br>组添加成功，但角色添加到组失败：" + result.ResultDescription;
                        }
                    }
                    if (this.cbxAddMeToGroup.get_Checked() && Securitor.GroupManager.AddUserToGroup(Securitor.UserManager.GetUserNoByUserName(Securitor.Authentication.Identity.Name), group.GroupNo).IsFailed)
                    {
                        resultDescription = resultDescription + "，但添加用户到此组失败";
                    }
                }
                this.lblMessage.set_Text(resultDescription);
            }
        }

        protected override void InitData()
        {
            base.InitData();
            this.groupNo = base.get_Request().get_Params()["GroupNo"];
            this.parentNo = base.get_Request().get_Params()["ParentNo"];
            this.newItem = Checker.CheckEmptyString("PermissionNo", this.groupNo, false);
            if (!base.get_IsPostBack())
            {
                this.lbxAailRoles.set_DataTextField("Title");
                this.lbxAailRoles.set_DataValueField("RoleNo");
                this.lbxAailRoles.set_DataSource(Securitor.RoleManager.ListItem());
                this.lbxAailRoles.DataBind();
                if (!this.newItem)
                {
                    Group item = Securitor.GroupManager.GetItem(this.groupNo);
                    if (item == null)
                    {
                        WebHelper.ShowMessage("没找到此组，或者您没有权限操作", "history.back()", true, true);
                    }
                    this.txtGroupNo.set_Text(item.GroupNo);
                    this.txtGroupNo.set_ReadOnly(true);
                    DataTransfer.TransEntityPropertyToWebFormData(item, this);
                    string[] groupRoles = Securitor.GroupManager.GetGroupRoles(this.groupNo);
                    this.lbxRoles.set_DataTextField("Title");
                    this.lbxRoles.set_DataValueField("RoleNo");
                    this.lbxRoles.set_DataSource(Securitor.RoleManager.GetItem(groupRoles));
                    this.lbxRoles.DataBind();
                    this.cbxAddMeToGroup.set_Checked(false);
                    this.cbxAddMeToGroup.set_Enabled(false);
                }
                else
                {
                    this.hlkParentNo.set_Text(this.parentNo);
                }
                this.hlkParentNo.set_NavigateUrl(string.Format("groupEdit.aspx?GroupNo={0}", this.hlkParentNo.get_Text()));
            }
        }

        private void InitializeComponent()
        {
            this.btnRemoveRoles.add_Click(new EventHandler(this.btnRemoveRoles_Click));
            this.btnAddRoles.add_Click(new EventHandler(this.btnAddRoles_Click));
            this.btnSubmit.add_Click(new EventHandler(this.btnSubmit_Click));
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
        }
    }
}

